define(['angularAMD'], function (angularAMD) {
	angularAMD.service('B0503Service', ['$rootScope', '$filter', '$stateParams', 'config', 'MMWService', '$calculater', '$mdDialog',
		function ($rootScope, $filter, $stateParams, config, $MMWService, $calculater, $mdDialog) {
			var service = this;
			service.genController = function(options, controller){
				controller.init = function(){
					// controller.condition = {
					// 	qc_type: '首检',
					// 	item_no: '000',
					// 	plot_no: '511-123456789',
					// 	spec: 'TEST_ITEM',
					// 	op_no: '1001'
					// };
					controller.condition = {}; //查詢條件
					controller.pqcInfo = []; //查出的檢驗單資訊
					controller.itemInfo = {}; //目前選擇的檢驗項目

					if(options.condition){
						controller.condition = options.condition;
						controller.search();
					}

					if(controller.paraData){
						controller.search(controller.paraData);
					}
				};

				controller.search = function(para){
					var content = {
						qc_type: controller.condition.qc_type || '',
						qc_form_no: controller.condition.qc_form_no || '',
						item_no: controller.condition.item_no || '',
						op_no: controller.condition.op_no || '',
						spec: controller.condition.spec || '',
						plot_no: controller.condition.plot_no || '',
						position_no: controller.condition.position_no || '',
					};
					if(para){
						controller.condition.qc_type = para.qc_type;
						controller.condition.qc_name = $filter('translate')(`B0503.qc_type_${para.qc_type}`)
						controller.condition.qc_form_type_name = $filter('translate')(`B0503.qc_form_type_${para.qc_form_type}`)
						controller.condition.qc_form_type = para.qc_form_type; 
						controller.condition.item_no = para.item_no;
						controller.condition.op_no = para.op_no;
						controller.condition.spec = para.spec;
						controller.condition.plot_no = para.plot_no;
						controller.condition.position_no = para.position_no;
						controller.content = para;
					}
					controller.content = content;
					pqcInfoGet(content, function(item){
						controller.pqcInfo = item.pqc_info_get;
						
						//自動勾選參數傳入的檢驗單編號與檢驗項目，否則預設選取第一筆檢驗單編號
						if(options.itemInfo && options.itemInfo.qc_form_no && options.itemInfo.qc_form_no != ''){
							for(var i = 0; i < controller.pqcInfo.length; i++){
								if(options.itemInfo.qc_form_no == controller.pqcInfo[i].qc_form_no){
									controller.clickItemInfo(controller.pqcInfo[i]);
									controller.pqcInfo[i].qc_item_detail.forEach(function(qcItem, index){
										qcItem.isSelect = options.itemInfo.qc_item_detail[index].isSelect;
									});
									break;
								}
							}
						}else{
							controller.clickItemInfo(controller.pqcInfo[0]);
						}
					});
				};

				controller.clear_condition = function(){
					controller.condition = {};
				};

				controller.clickItemInfo = function(item){
					controller.itemInfo = item;
					controller.qcSelectAll = false;
					if(controller.pqcInfo && controller.pqcInfo.length > 0){
						controller.itemInfo.qc_item_detail.forEach(function(qc_item){
							qc_item.isSelect = controller.qcSelectAll;
						});
					}
				};

				//讀取生產批
				controller.loadPlotList = function(){
					$MMWService.showQueryListPNIG({
						page_mode: 3,
						pageRecord:50,
						condition: [
							{name:$filter('translate')('common.queryList.plot_no'), column_name:'LotNo', query_mode:'2', value:controller.condition.plot_no}
						],
						header: [
							{label:$filter('translate')('common.queryList.plot_no'), code:'page_no'}
						],
						setCondition:function(conditions){
							conditions.push({column_name:'status', query_mode:'0', value:'0', merge_condition_model:'and'});
							conditions.push({column_name:'status', query_mode:'0', value:'1', merge_condition_model:'and'});
							conditions.push({column_name:'status', query_mode:'0', value:'2', merge_condition_model:'and'});
							return conditions;
						},
						selectCode:controller.condition.plot_no,
						confirm : function(selectItem){
							controller.condition.plot_no = selectItem.page_no;
						}
					});
				};
				
				//讀取檢驗類型 1：出站檢驗 2：首檢 3：巡檢 4：工位檢
				controller.loadQcType = function(){
					var typeList = [
						{code : '1' , name: $filter('translate')('B0503.qc_type_1')},
						{code : '2' , name: $filter('translate')('B0503.qc_type_2')},
						{code : '3' , name: $filter('translate')('B0503.qc_type_3')},
						{code : '4' , name: $filter('translate')('B0503.qc_type_4')}
					];
					$rootScope.showSelect({
						title : $filter('translate')('common.msg.plz_select'),
						label : 'name',
						code : 'code',
						order : 'code',
						selectCode : controller.condition.qc_type || '',
						list : typeList,
						confirm : function(selectItem, TypeDialog){
							controller.condition.qc_type = selectItem.code;
							controller.condition.qc_name = selectItem.name;
							TypeDialog.hide();
						}
					});
				};

				//讀取品號
				controller.loadItemNo = function(){
					$rootScope.showQueryList({
						key:'item_no',
						header : [
							{label: $filter('translate')('B0504.queryList.item_no'), code:'item_no'},
							{label: $filter('translate')('B0504.queryList.item_name'), code:'item_name'},
							{label: $filter('translate')('B0504.queryList.item_spec'), code:'item_spec'},
							{label: $filter('translate')('B0504.queryList.qc_category'), code:'qc_category'},
							{label: $filter('translate')('B0504.queryList.article_no'), code:'article_no'},
							{label: $filter('translate')('B0504.queryList.unit'), code:'unit'}
						],
						query: function(conditions, feedback){
							var content = {
								qry_type: 0 //0：產品  1：物料
							}
							productInfoGet(content , function(data){
								if(data.product_info_get){
									feedback(data.product_info_get);
								}
							});
						},
						confirm: function(selectItem, dialog){
							dialog.hide();
							controller.condition.item_no = selectItem.item_no; //品號編號
						}
					});
				};

				//選擇作業站
				controller.loadOpNo = function(){
					$rootScope.showQueryList({
						//20210527 modify by kaiinagami for M#92576: 修正QueryList的Key值
						key:'page_no',
						header : [
							{label: $filter('translate')('common.queryList.op_c'), code:'page_no'},
							{label: $filter('translate')('common.queryList.op_n'), code:'page_name'}
						],
						query: function(conditions, feedback){
							$MMWService.pageNumberInfoGet({
								page_mode : '4'
							}, function(itemList){
								if(itemList.length != 0){
									feedback(itemList);
								}else{
									feedback();
								}
							});
						},
						selectCode:controller.condition.op_no||'',
						confirm: function(selectItem, dialog){
							controller.condition.op_no = selectItem.page_no;
							controller.condition.op_name = selectItem.page_name;
							dialog.hide();
						},
						cancel:function(dialog){
							controller.condition.op_no = '';
							controller.condition.op_name = '';
							dialog.hide();
						}
					});
				};

				controller.confirm = function(){
					$rootScope.hideMenu();
					$rootScope.changeProgram('B0503_1', {data: {
						condition: controller.condition, //查詢條件
						itemInfo: controller.itemInfo, //目前選擇的檢驗項目
						content: controller.content,//confirm時紀錄送出資料，方便回B0503_1回上一頁時人員操作
					}});
				};

				controller.qcItemSelect = function(event, index, item){
					event.stopPropagation();
					item.isSelect = !item.isSelect;
				};

				controller.qcItemSelectAll = function(){
					controller.qcSelectAll = !controller.qcSelectAll;
					controller.itemInfo.qc_item_detail.forEach(function(qc_item){
						qc_item.isSelect = controller.qcSelectAll;
					});
				};

				controller.isSelectQcItem = function(){
					var isSelect = false;
					if(controller.itemInfo && controller.itemInfo.qc_item_detail){
						for(var i = 0; i < controller.itemInfo.qc_item_detail.length; i++){
							if(controller.itemInfo.qc_item_detail[i].isSelect){
								isSelect = true;
								break;
							}
						}
					}
					return isSelect;
				};

				return controller;
			};

			service.init = function(options){
				$mdDialog.dialog(
					'module/modulesSPC/program/B0503/B0503.tmp.html',
					function(dialog){
						var controller = service.genController(options, dialog);
						controller.css_url = 'module/modulesSPC/program/B0503/B0503.css';
						controller.beforeShown = function(){
							dialog.init();
							dialog.search();
						};
						controller.back = function () {
							dialog.hide();
						};
						controller.dialog_confirm = function () {
							if(options.feedback)
								options.feedback(dialog);
						};
						return controller;
					}
				);
			};
			
			//品號資訊取得
			function productInfoGet(content, callback){
				// var data = [];
				// if(content.type == 0){
				// 	data = [{
				// 		product_type: '品號類別A',
				// 		desc: '產品編號第A類',
				// 	},{
				// 		product_type: '品號類別B',
				// 		desc: '產品編號第B類',
				// 	}];
				// }else{
				// 	data = [{
				// 		product_type: '品號類別A',
				// 		desc: '產品編號第A類',
				// 		item_no: '000',
				// 		item_name: '測試用品號000',
				// 		item_spec: '30cm',
				// 		qc_category: '品管a類',
				// 		article_no: '999999'
				// 	},{
				// 		product_type: '品號類別B',
				// 		desc: '產品編號第B類',
				// 		item_no: '001',
				// 		item_name: '測試用品號001',
				// 		item_spec: '31cm',
				// 		qc_category: '品管b類',
				// 		article_no: '666666'
				// 	}];
				// }
				
				// if(callback)
				// 	callback(data);
				$MMWService.sendToSMESServer({
					uri: 'tcSPC.clsSpc.product_info_get',
					content : content,
					success : function(data){
						if(callback)
							callback(data);
					}
				});
			}

			//PQC檢驗單資訊取得
			function pqcInfoGet(content, callback){
				// var data = {};
				// data.pqc_info_get = [
				// 	{
				// 		qc_form_no: 'TEST-123456789',
				// 		item_no: '000',
				// 		item_name: '测试用品号',
				// 		plot_no: '511-123456789',
				// 		op_no: '1001',
				// 		qc_item_detail: [
				// 			{
				// 				qc_item: '項目一',
				// 				qc_type: '1',
				// 				status: '0',
				// 				qc_result: '1'
				// 			},{
				// 				qc_item: '項目二',
				// 				qc_type: '0',
				// 				status: '1',
				// 				qc_result: '1'
				// 			},
				// 			{
				// 				qc_item: '項目三',
				// 				qc_type: '0',
				// 				status: '1',
				// 				qc_result: '1'
				// 			}
				// 		]
				// 	},{
				// 		qc_form_no: 'TEST-987654321',
				// 		item_no: '000',
				// 		item_name: '测试用品号',
				// 		plot_no: '511-987654321',
				// 		op_no: '1001',
				// 		qc_item_detail: [
				// 			{
				// 				qc_item: '項目四',
				// 				qc_type: '1',
				// 				status: '1',
				// 				qc_result: '0'
				// 			},{
				// 				qc_item: '項目五',
				// 				qc_type: '0',
				// 				status: '0',
				// 				qc_result: '0'
				// 			},
				// 			{
				// 				qc_item: '項目六',
				// 				qc_type: '0',
				// 				status: '0',
				// 				qc_result: '1'
				// 			}
				// 		]
				// 	}
				// ]
				// if(callback)
				// 	callback(data);
				$MMWService.sendToSMESServer({
					uri: 'tcSPC.clsSpc.pqc_info_get',
					content : content,
					success : function(data){
						if(callback)
							callback(data);
					}
				});
			}
		}
	]);
});
